package com.pronhub.luowowo.mgrsite.timejob;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pronhub.luowowo.article.service.IStrategyDetailService;
import com.pronhub.luowowo.article.vo.StrategyPersistenceVO;
import com.pronhub.luowowo.service.IStrategyStatisRedisService;
import com.pronhub.luowowo.util.BeanUtilLXY;
import com.pronhub.luowowo.util.Rediskey;
import com.pronhub.luowowo.vo.StrategyStatisVO;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时器 就是保证在多长时间内进行一次Redis数据的持久化保存
 *
 */
//@Component
public class RedisDataPersistenceJob{

    //1读取Redis的数据
    //2进行固化操作
    //Redis的操作
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    //持久化操作
    @Reference
    private IStrategyDetailService strategyDetailService;
    //http://cron.qqe2.com/


    @Scheduled(cron = "0/10 * * * * ? ")
    public void doWork(){
        //查出Redis里面的所有文章的VO 因为有个方法
        List<StrategyStatisVO> list = strategyStatisRedisService.getStrategyStatisVOS(Rediskey.
                STRATEGY_STATIS_VO.getKeys("*"));

        //持久化到MySQL数据库里面
        for (StrategyStatisVO vo : list) {
            //进行替换
            StrategyPersistenceVO vo1 = new StrategyPersistenceVO();
            BeanUtilLXY.copyProperties(vo1,vo);
            strategyDetailService.updateDataByRedisVO(vo1);
            System.err.println(new Date().toLocaleString());
        }
    }
}
